"""
此方法将多个yaml文件测试用例，都汇总到一个allure测试报告里面去
"""
import os
import subprocess
from common.Del_old_Uril.file_remove import remove_file
from common.Del_old_Uril.del_log_util import delete_old_logs

# 当前路径(使用 abspath 方法可通过dos窗口执行)
current_path = os.path.dirname(os.path.abspath(__file__))
# json报告路径
json_report_path = os.path.join(current_path, './outFiles/report/json')
# html报告路径
html_report_path = os.path.join(current_path, './outFiles/report/html')

if __name__ == '__main__':
    # 运行前删除旧的report测试报告
    remove_file(file_path="outFiles/report/")
    # 保留days_to_keep=3天日志，超过3天自动清除
    delete_old_logs(log_dir='outFiles/logs/', days_to_keep=3)

    # 定义要执行的YAML用例文件，可指定多个执行
    yaml_files = [
        # 'data/ctrler_yaml/UAT/test_vni_a.yaml',
        # 'data/ctrler_yaml/UAT/test_vni_b.yaml',
        # 'data/ctrler_yaml/test_logic_port.yaml',
        # 'data/ctrler_yaml/test_l2lan_a.yaml',
        # 'data/ctrler_yaml/test_l2lan_b.yaml',
        # 'data/ctrler_yaml/test_l2lan_c.yaml',
        # 'data/ctrler_yaml/test_l2lan_d.yaml',
        # 'data/ctrler_yaml/test_l2lan_update_bandwidth_a.yaml',
        # 'data/ctrler_yaml/test_l2lan_update_bandwidth_b.yaml',
        # 'data/ctrler_yaml/test_path_create_del_a.yaml',
        # 'data/ctrler_yaml/test_path_create_del_b.yaml',
        # 'data/ctrler_yaml/test_path_create_del_c.yaml',
        # 'data/ctrler_yaml/test_path_create_del_d.yaml'
    ]

    # 创建一个统一的JSON报告目录和HTML报告目录
    os.makedirs(json_report_path, exist_ok=True)
    os.makedirs(html_report_path, exist_ok=True)

    for yaml_file in yaml_files:
        # 设置环境变量，指定当前测试用例文件执行
        os.environ['TEST_CASES_FILE'] = yaml_file

        print(f"_____Run Test_____：{yaml_file}")

        # 使用subprocess调用pytest
        cmd = f'pytest -vs ./testCase/test_case_driver.py --alluredir={json_report_path}'
        subprocess.run(cmd, shell=True)

    # 生成汇总的Allure报告
    cmd = f'allure generate {json_report_path} -o {html_report_path} --clean'
    subprocess.run(cmd, shell=True)